package web

type Exception struct {
	Id  int    // exception id
	Msg string // exception msg
}

type TryStruct struct {
	catche func(interface{})
	try    func()
}

func Try(tryHandler func()) *TryStruct {
	tryStruct := TryStruct{
		catche: nil,
		try:    tryHandler,
	}
	return &tryStruct
}

type ExceptionHandler func(interface{})

func (this *TryStruct) Catch(catch func(interface{})) *TryStruct {
	this.catche = catch
	return this
}

func (this *TryStruct) Finally(finally func()) {
	defer func() {
		if e := recover(); nil != e {
			if this.catche != nil {
				this.catche(e)
			}
		}

		finally()
	}()

	this.try()
}

func Throw(id int, msg string) Exception {
	panic(Exception{id, msg})
}
